using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using GameLib.GameItem;

namespace GameLib.GameScreen
{
    class GameLevelScreen : GameScreen
    {
        ItemManager itemManager;
        Texture2D background;

        public GameLevelScreen(ScreenManager manager)
            : base(manager)
        {
            Load();
        }

        public override void Load()
        {
            background = content.Load<Texture2D>(@"Images\background");
            itemManager = new ItemManager();

            SpriteFont levelFont = content.Load<SpriteFont>(@"Fonts\LevelFont");

            Texture2D btn_Level_0 = content.Load<Texture2D>(@"Images\btn_Level_0");
            Texture2D btn_Level_2 = content.Load<Texture2D>(@"Images\btn_Level_2");
            Texture2D archive_0 = content.Load<Texture2D>(@"Images\archive_0");
            Texture2D archive_3 = content.Load<Texture2D>(@"Images\archive_3");
            Texture2D archive;

            LevelButton[] levelButtons = new LevelButton[40];
            int x = 60;
            int y = 70;

            int start;

            if (GameData.LoadGame())
            {
                start = Convert.ToInt32(GameData.SaveData.Level);
            }
            else
            {
                start = 5;
                GameData.GenerateDefault();
                GameData.SaveGame();
            }

            for (int i = 0; i < start; i++)
            {
                if (GameData.SaveData.GameResult[i].Star == "0")
                {
                    archive = archive_0;
                }
                else
                {
                    archive = archive_3;
                }

                LevelButton level = new LevelButton(btn_Level_0,
                                                    archive,
                                                    new Vector2(x, y),
                                                    (i + 1).ToString(),
                                                    levelFont,
                                                    new Color(0, 54, 123),
                                                    itemManager);
                level.ItemPress += new EventHandler(btn_Level_Press);
                itemManager.AddItem(level);

                if ((i + 1) % 10 != 0)
                {
                    x += 70;
                }
                else
                {
                    x = 60;
                    y += 90;
                }
            }

            for (int i = start; i < 40; i++)
            {
                LevelButton level = new LevelButton(btn_Level_2,
                                                    null,
                                                    new Vector2(x, y),
                                                    null,
                                                    null,
                                                    Color.White,
                                                    itemManager);
                //level.ItemPress += new EventHandler(btn_Level_Press);
                itemManager.AddItem(level);

                if ((i + 1) % 10 != 0)
                {
                    x += 70;
                }
                else
                {
                    x = 60;
                    y += 90;
                }
            }

            GameButton btn_Back = new GameButton(content.Load<Texture2D>(@"Images\back"), new Vector2(740, 15), null, null, itemManager);
            btn_Back.ItemPress += new EventHandler(btn_Back_Press);
            itemManager.AddItem(btn_Back);

            this.LoadComplete = true;
        }

        public override void UnLoad()
        {
            itemManager.Clear();
            base.UnLoad();
        }

        public override void Update(GameTime gametime)
        {
            TouchCollection touchCollection = TouchPanel.GetState();
            foreach (TouchLocation touch in touchCollection)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    itemManager.CheckPress(touch.Position);
                }
            }

            itemManager.Update(gametime);
            base.Update(gametime);
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(background, Vector2.Zero, Color.White);
            itemManager.Draw(spritebatch);
            base.Draw(spritebatch);
        }

        private void btn_Level_Press(object sender, EventArgs e)
        {
            manager.RemoveAndAdd(this, ScreenName.GamePlay);
        }

        private void btn_Back_Press(object sender, EventArgs e)
        {
            manager.RemoveAndAdd(this, ScreenName.GameTitle);
        }
    }
}
